﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Specialized;

/// <summary>
/// Summary description for Mahoa
/// </summary>
public class Mahoa
{
    private byte[] lbtVector = new byte[]{240, 3, 45, 29, 0, 76, 173, 59};
    private string lscriptoKey = "DMB238373";

    
	public Mahoa()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public string MahoaMD5(string Chuoimahoa)
    {
        byte[] arr = System.Text.Encoding.UTF8.GetBytes(Chuoimahoa);
        MD5CryptoServiceProvider cp = new MD5CryptoServiceProvider();
        arr = cp.ComputeHash(arr);
        
        string str = "";
        foreach (byte b in arr)
        {
            str += b.ToString("X2");
        }
        return str;
    }

    public string Giai_ma(string cautv)
    {
        Byte[] buffer;
        TripleDESCryptoServiceProvider loCriptoClass = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider loCriptoProvider = new MD5CryptoServiceProvider();
        try
        {
            buffer = Convert.FromBase64String(cautv);
            loCriptoClass.Key = loCriptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscriptoKey));
            loCriptoClass.IV = lbtVector;
            return Encoding.ASCII.GetString(loCriptoClass.CreateDecryptor().TransformFinalBlock(buffer,0,buffer.Length));
            
        }
        catch (Exception e)
        {
            
            //Catch ex As Exception
            throw e;
        }
         finally
        {
            loCriptoClass.Clear();
            loCriptoProvider.Clear();
            loCriptoClass = null;
            loCriptoProvider = null;
            
        }
    }


    public string Ma_hoa(String chuoi_mh)
    {
         TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();
         MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();
         byte[] chuoi_t;

         try
         {
             chuoi_t = System.Text.Encoding.ASCII.GetBytes(chuoi_mh);
             loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscriptoKey));
             loCryptoClass.IV = lbtVector;
             chuoi_mh = Convert.ToBase64String(loCryptoClass.CreateEncryptor().TransformFinalBlock(chuoi_t, 0, chuoi_t.Length));
             return chuoi_mh;
         }
         catch (CryptographicException e)
         { throw e; }
         catch (FormatException e)
         { throw e; }
         catch (Exception e)
         {
             throw e;
         }
            
            finally
        {
        loCryptoClass.Clear();
        loCryptoProvider.Clear();
        loCryptoClass = null;
        loCryptoProvider = null;
        }

    }

//   Try This function.......for both Decrypt and Encrypt also....

//Public Function psDecrypt(ByVal sQueryString As String) As String

//Dim buffer() As Byte
//Dim loCryptoClass As New TripleDESCryptoServiceProvider
//Dim loCryptoProvider As New MD5CryptoServiceProvider

//Try

//buffer = Convert.FromBase64String(sQueryString)
//loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
//loCryptoClass.IV = lbtVector
//Return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer 0 buffer.Length()))
//Catch ex As Exception
//Throw ex
//Finally
//loCryptoClass.Clear()
//loCryptoProvider.Clear()
//loCryptoClass = Nothing
//loCryptoProvider = Nothing
//End Try

//End Function

//Public Function psEncrypt(ByVal sInputVal As String) As String
//Dim loCryptoClass As New TripleDESCryptoServiceProvider
//Dim loCryptoProvider As New MD5CryptoServiceProvider
//Dim lbtBuffer() As Byte

//Try
//lbtBuffer = System.Text.Encoding.ASCII.GetBytes(sInputVal)
//loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
//loCryptoClass.IV = lbtVector
//sInputVal = Convert.ToBase64String(loCryptoClass.CreateEncryptor().TransformFinalBlock(lbtBuffer 0 lbtBuffer.Length()))
//psEncrypt = sInputVal
//Catch ex As CryptographicException
//Throw ex
//Catch ex As FormatException
//Throw ex
//Catch ex As Exception
//Throw ex
//Finally
//loCryptoClass.Clear()
//loCryptoProvider.Clear()
//loCryptoClass = Nothing
//loCryptoProvider = Nothing
//End Try
//End Function


    

//If you are calling ToString on a byte to convert it to a 2 digit hex value does it matter if you use a format provide of CultureInfo.CurrentCulture or CultureInfo.InvariantCulture in any way?

//Example:

//public string CalculateMD5Hash(string input)
//{
//    MD5 md5 = System.Security.Cryptography.MD5.Create();
//    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
//    byte[] hash = md5.ComputeHash(inputBytes);

//    StringBuilder sb = new StringBuilder();
//    foreach (byte b in hash)
//    {
//        sb.Append(b.ToString("x2",CultureInfo.InvariantCulture));
//    }
//    return sb.ToString();
//}
//Basically can I leave out the IFormatProvider and always get the same result, or if I provide on of the CultureInfo defaults it will make a difference in the output?


}